<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb>
        <el-breadcrumb-item>
          <i class="el-icon-tickets">渠道详情</i>
        </el-breadcrumb-item>
      </el-breadcrumb>
      <div class="cantainer">
        <div class="handle-box">
          <el-button type="primary" @click="dialogFormVisible = true" class="handle-del mr10">添加</el-button>
          <el-select v-model="forwardId" placeholder="请选择货代" style="width: 180px">
            <el-option
              v-for="item in freightForwardList"
              :key="item.forwardId"
              :label="item.name"
              :value="item.forwardId">
            </el-option>
          </el-select>
          <el-button type="primary" icon="el-icon-search" @click="searchData">搜索</el-button>
          <el-button type="danger" @click="batchDelete" class="handle-del mr10">批量删除</el-button>
          <!--          <el-input class="search-input" placeholder="筛选关键词" size="small" v-model="ListSearch"></el-input>-->
        </div>
        <el-table
          :data="list.slice(0,pagesize)"
          tooltip-effect="dark"
          style="width: 100%"
          v-loading="addLoading"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column
            prop="channelName"
            label="渠道名称"
            width="150"
            sortable
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click.stop="handleEdit(scope.$index, scope.row)"
              >编辑
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click.stop="clickDel(scope.$index,scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="el-pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 40]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCount"
          background>
        </el-pagination>
      </div>
    </div>

    <!--编辑弹出框-->
    <el-dialog title="编辑" :visible.sync="editFormVisible" width="20%" center>
      <el-form :model="editForm" label-width="50px" ref="addForm">
        <el-form-item label="渠道名称" label-width="80px">
          <el-input v-model="editForm.channelName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSubmit" :loading="addLoading">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 添加弹出框 -->
    <el-dialog title="信息添加" :visible.sync="dialogFormVisible" width="20%" @close="closeAdd" center>
      <el-form :model="form" label-width="80px">
        <el-form-item label="货代信息" prop="region">
          <el-select v-model="addForwardId" placeholder="请选择货代" style="width: 180px">
            <el-option
              v-for="item in freightForwardList"
              :key="item.forwardId"
              :label="item.name"
              :value="item.forwardId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="渠道名称" label-width="80px">
          <el-input v-model="form.channelName" style="width: 180px;"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer ">
        <el-button @click="closeAdd">取 消</el-button>
        <el-button type="primary" @click="add" :loading="addLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "../../../util/axios_config";
import {getFreightForwardData, getChannelData} from "../../../util/common.js";

export default {
  data() {
    return {
      list: [], //数据存储
      dialogFormVisible: false,   //添加弹出框
      ListSearch: "",  //搜索
      editFormVisible: false,  //编辑器弹出框
      form: {    //需要添加的字段
        channelName: ""
      },
      editForm: {   //编辑需要的字段
        channelName: ""
      },
      addLoading: false,
      currentPage: 1, //初始页
      pagesize: 5,    //	每页的数据
      totalCount: 0,
      freightForwardList: [],//单页数据
      forwardId: "",//搜索货代id
      addForwardId: "",//增加货代id
      delList: [],
    }
  },
  created() {
    // this.vueTable()
    //获取货代信息
    getFreightForwardData().then(data => {
      this.freightForwardList = data;
    }).catch(error => {
      this.$message.error(error.message);
    });
  },

  methods: {
    // 改变条数
    handleSizeChange: function (size) {
      this.pagesize = size;
      this.currentPage = 1; // 重置到第一页
      this.searchData();
    },
    //改变当前页
    handleCurrentChange: function (currentPage) {
      this.currentPage = currentPage;
      this.searchData();
    },
    // 显示编辑弹出框
    handleEdit(index, row) {
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);
    },
    // 编辑
    editSubmit(row) {
      if (!this.editForm.channelId || !this.editForm.channelName) {
        this.$message.error('内容不能为空');
      } else {
        let HandleEdit = {
          channelId: this.editForm.channelId,
          channelName: this.editForm.channelName
        }
        axios.put("/channel/update", HandleEdit).then(res => {
          this.addLoading = false;
          this.editFormVisible = false;
          if (res.data) {
            var data = {};
            if (typeof res.data === 'string') {
              data = JSON.parse(res.data);
            } else {
              data = res.data;
            }
            if (data.errCode === 10000) {
              this.$message.success('添加成功');
              this.searchData();
            } else {
              this.$message.error(data.errMsg);
            }
          }
        })
      }

    },
    clickDel(index, row) {
      this.delList.push(row.channelId);
      this.deleteList();
    },
    // 删除
    deleteList() {
      this.$confirm('此操作将永久删除该货代及渠道数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const uniqueArray = this.delList.filter((item, index) => this.delList.indexOf(item) === index);
        let HandleDel = {
          channelIdAttr: uniqueArray,
        }
        axios.post("/channel/del", HandleDel).then(res => {
          if (res.data) {
            var data = {};
            if (typeof res.data === 'string') {
              data = JSON.parse(res.data);
            } else {
              data = res.data;
            }
            if (data.errCode === 10000) {
              this.$message.success('删除成功');
              this.searchData();
              this.delList = [];
            } else {
              this.$message.error(data.errMsg);
            }
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 添加数据
    add() {
      if (!this.form.channelName) {
        this.$message.error('内容不能为空');
      } else {
        let handleAddClick = {
          forwardId: this.addForwardId,
          channelName: this.form.channelName
        }
        axios.post("channel/add", handleAddClick).then(res => {
          this.dialogFormVisible = false;
          if (res.data) {
            var data = {};
            if (typeof res.data === 'string') {
              data = JSON.parse(res.data);
            } else {
              data = res.data;
            }
            if (data.errCode === 10000) {
              this.$message.success('添加成功');
              if(this.forwardId){
                this.searchData();
              }
            } else {
              this.$message.error(data.errMsg);
            }
          }
        })
      }
    },
    //搜索渠道
    searchData() {
      if (!this.forwardId) {
        this.$message.error('请先选择货代');
      } else {
        this.addLoading = true;
        //获取渠道数据
        getChannelData(this.forwardId, this.currentPage, this.pagesize).then(data => {
          this.addLoading = false;
          this.list = data['list'];
          if (this.currentPage <= 1) {
            this.totalCount = data['totalCount'];
          }
        }).catch(error => {
          this.addLoading = false;
          this.$message.error(error.message);
        });

      }
    },
    //关闭添加表单
    closeAdd() {
      this.form.channelName = "";
      this.addForwardId = "";
      this.dialogFormVisible = false;
    },
    //多选框
    handleSelectionChange(val) {
      this.delList = val.map(item => item.channelId);
    },
    //批量删除
    batchDelete() {
      if (this.delList.length <= 0) {
        this.$message.error('请选择要删除的数据');
      } else {
        this.deleteList()
      }
    }
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.search-input {
  width: 200px;
  margin-left: 50px;
}

.el-table__body-wrapper {
  height: 500px;
}

.crumbs {
  margin-bottom: 20px;
}
.el-pagination {
  text-align: center;
}
</style>
